package com.briup.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.briup.DTO.AddOrUpdateQuarantimeRegistraionDTO;
import com.briup.DTO.SaveOrUpdateDiseaseRecordDTO;
import com.briup.VO.DiseaseRecordPageVO;
import com.briup.constant.SystemConstant;
import com.briup.enums.CodeAndMessageEnum;
import com.briup.exception.SystemException;
import com.briup.mapper.DiseaseMapper;
import com.briup.mapper.DiseaseRecordMapper;
import com.briup.pojo.Disease;
import com.briup.pojo.DiseaseRecord;
import com.briup.pojo.QuarantineRegistration;
import com.briup.result.PageTemplate;
import com.briup.result.Result;
import com.briup.service.DiseaseRecordService;
import com.briup.service.DiseaseService;
import com.briup.util.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author Lenovo
 * @description 针对表【disease_record】的数据库操作Service实现
 * @createDate 2024-03-19 10:44:51
 */
@Service
public class DiseaseRecordServiceImpl extends ServiceImpl<DiseaseRecordMapper, DiseaseRecord>
        implements DiseaseRecordService {
    @Autowired
    private DiseaseRecordMapper diseaseRecordMapper;

    @Override
    public PageTemplate qrquery(Integer pageNum, Integer pageSize, String drDId, String drStatus) throws SystemException {
        //参数校验
        if (Objects.isNull(pageNum) || Objects.isNull(pageSize)) {
            //页码或页面大小为空
            throw new SystemException(CodeAndMessageEnum.PAGE_NUMBER_OR_PAGE_SIZE_IS_NULL);
        }
        Page<DiseaseRecordPageVO> DPage = new Page<>(pageNum, pageSize);
        IPage<DiseaseRecordPageVO> diseaseRecordPageVOIPage=diseaseRecordMapper.queryList(DPage,drDId,drStatus);
        return new PageTemplate(diseaseRecordPageVOIPage.getTotal(),diseaseRecordPageVOIPage.getRecords());
    }
    @Override
    public Result deleteById(String id) throws SystemException {
        DiseaseRecord diseaseRecord = diseaseRecordMapper.selectById(id);
        if (Objects.isNull(diseaseRecordMapper)) {
            throw new SystemException(CodeAndMessageEnum.DISEASE_RECORD_IS_NULL);
        }
        diseaseRecordMapper.deleteById(id);
        return Result.success();
    }

    @Override
    public Result deleteByIdAll(List<String> ids) throws SystemException {
        LambdaQueryWrapper<DiseaseRecord> diseaseRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
        diseaseRecordLambdaQueryWrapper.in(Objects.nonNull(ids) && !ids.isEmpty(), DiseaseRecord::getDrDId, ids);
        List<DiseaseRecord> fenceHouses = diseaseRecordMapper.selectList(diseaseRecordLambdaQueryWrapper);
        if (ids.size() != fenceHouses.size()) {
//            有id找不到对应的信息
            throw new SystemException(CodeAndMessageEnum.DISEASE_RECORD_IS_NULL);
        }
        diseaseRecordMapper.deleteBatchIds(ids);
        return Result.success();
    }

    @Override
    public Result DRSaveOrUpdate(SaveOrUpdateDiseaseRecordDTO saveOrUpdateDiseaseRecordDTO) throws SystemException {
        //参数校验
        if (Objects.isNull(saveOrUpdateDiseaseRecordDTO.getDrAnimalId()) || !StringUtils.hasText(saveOrUpdateDiseaseRecordDTO.getDrAnimalId())) {
            throw new SystemException(CodeAndMessageEnum.ANIMAL_ID_IS_EXIST);
        }
        //bean拷贝
        DiseaseRecord diseaseRecord = BeanCopyUtils.copyBean(saveOrUpdateDiseaseRecordDTO, DiseaseRecord.class);

        if (Objects.isNull(saveOrUpdateDiseaseRecordDTO.getDrId())) {
            diseaseRecordMapper.insert(diseaseRecord);
            return Result.success();
        } else {
            diseaseRecordMapper.updateById(diseaseRecord);
            return Result.success();
        }
    }
}